/**
 * GET请求，此请求在Header中添加参数
 * @param {Object} action 不包含主机域名的url，如api/v1/blogs/page/
 * @param {Object} header 请求头，包含参数，若无需参数请使用其他版本的get函数
 * @param {Object} funSuccess 请求成功的回调
 * @param {Object} funConplete 请求结束的回调，若无需处理则传空方法体
 */
function getwithParams(url, header, funSuccess, funComplete = ()=>{}) {
	uni.showLoading({
		title: '加载中....',
		mask: false
	});
	uni.request({
		url: url,
		method: 'GET',
		header: header,
		sslVerify: false,
		success: res => {
			funSuccess(res)
		},
		fail: err => {
			console.log(err)
		},
		complete: () => {
			uni.hideLoading()
			funComplete()
		}
	});
}

/**
 * 无参的Get请求
 * @param {Object} url
 * @param {Object} header
 * @param {Object} funSuccess
 * @param {Object} funComplete
 */
function get(url, funSuccess, funComplete= ()=>{}){
	uni.showLoading({
		title: '加载中....',
		mask: false
	});
	uni.request({
		url: url,
		method: 'GET',
		header: {
			"content-type":"application/json;charset=UTF-8"
		},
		sslVerify: false,
		success: res => {
			funSuccess(res)
		},
		fail: err => {console.log(err)},
		complete:()=> {
			uni.hideLoading()
			funComplete()
		}
	});
}


function postParams(url,data,funSuccess,funComplete= () =>{}){
	uni.showLoading({
		title: '提交中....',
		mask: false
	});
	uni.request({
		url:url,
		header:{
			"accept":"application/json,*/*;q=0.1",
			"content-type":"application/x-www-form-urlencoded;charset=UTF-8"
		},
		method:'POST',
		data:data,
		sslVerify:false,
		success:res =>{
			funSuccess(res)
		},
		fail:err => {
			console.log(err);
		},
		complete:()=>{
			uni.hideLoading()
			funComplete()
		}
	});
}
function post(url,data,funSuccess,funComplete = () => {}) {
	uni.showLoading({
		title:'提交……',
		mask:false
	});
	uni.request({
		url:url,
		header:{
			"content-type": "application/json;charset=UTF-8"
		},
		method:'POST',
		data:data,
		sslVerify:false,
		success: res => {
			funSuccess(res.data)
		},
		fail: () => {},
		complete:() =>{
			uni.hideLoading()
			funComplete()
		}
	});
}
/**
 * 上传文件
 * @param {String} url 服务器地址
 * @param {String}
 * @param {String}
 * @param {JSON}
 * @param {String}
 * 
 */
function postFile(url,param,filePath,data,funSuccess,funComplete = ()=>{}){
	uni.showLoading({
		title:'提交中……',
		mask:false
	}),
	uni.uploadFile({
		url:url,
		sslVerify:false,
		filePath:filePath,
		name:param,
		formData:data,
		success: (res) => {
			funSuccess(res)
		},
		fail(err){
			console.log(err);
		},
		complete(){
			uni.hideLoading()
			funComplete()
		}
	})
}

/**
 * 以同步写法实现异步请求，在嵌套请求时，async块内部代码是同步的，会等待await代码行的执行结果
 * 注意lambda表达式内使用时，async应在匿名函数参数前（需要加括号）
 * 
 */
function request(url,data = {},method='GET'){
	return new Promise((resovle,reject) => {
		uni.request({
			url,
			method,
			data,
			success: res => {
				resovle(res.data)
			},
			fail:err =>{
				reject(err)
			}
		});
	})
}

/**
 * @param {String} url
 * @param {Josn} data
 * @param {Object} funSuccess
 */
function deleteParams(url,data,funSuccess){
	uni.request({
		url,
		method:'DELETE',
		data,
		header:{
			"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
		},
		success:res =>{ 
			funSuccess(res.data)
			},
		fail:err=>{
			console.log(err);
		},
		complete: () => {}
	});
}
module.exports ={
	getwithParams,
	get,
	postParams,
	post,
	postFile,
	request,
	deleteParams
}